Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
check whether a given property type is a typeof(list<>)

I have three lists something like List<EmpRoles> , List<EmpVisibility> , List<EmpProps>.

Now I want to perform certain operations on them. For this first I have to check whether the property is of type list or not. I have use if block something like below

if ( propertyName == "EmpRoles" || propertyName == "EmpVis" || propertyName == "EmpProps") 

Is there any better way of doing this thing , or is it possible to put some typeof(list<>) conditions. I know typeof(list<>) won't work here. Either i have to use typeof(list)... Can Someone help in making a generic way to identify the list type properties?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Checking that a type is a List<T> requires ensuring that:

  • the type is generic by checking its IsGenericType property,
  • the generic type base is System.List<> by checking the result of GetGenericTypeDefinition() against typeof(List<>)
  • type T is the one you want by checking GetGenericArguments()

If all three conditions are met, you have your type:

var pt = myProperty.PropertyType;
if (pt.IsGenericType &&
    pt.GetGenericTypeDefinition() == typeof(List<>)) {
    var elementType = pt.GetGenericArguments()[0];
    if (elementType == typeof(EmpRoles)) {
        ...
    } else if (elenentType == typeof(EmpVisibility)) {
        ...
    } else if ...
}
over 4 years ago · Santiago Trujillo Denunciar

0

You can get generic parameter from list as following

var list = new List<EmpRoles>
var argType = list.GetType().GenericTypeArguments[0]; 

Here in argType you will get typeof(EmpRoles)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda